package com.ruoyi.web.controller.system;

import java.util.List;

import com.ruoyi.common.utils.mysql.MysqlLink;
import com.ruoyi.system.domain.DatabaseDetails;
import com.ruoyi.system.service.IDatabaseDetailsService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.DabaTable;
import com.ruoyi.system.service.IDabaTableService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import static com.ruoyi.common.utils.Threads.sleep;

/**
 * 子数据库Controller
 *
 * @author ruoyi
 * @date 2022-06-15
 */
@Controller
@RequestMapping("/system/database")
public class DabaTableController extends BaseController {
    private String prefix = "system/database";


    @Autowired
    private IDatabaseDetailsService databaseDetailsService;

    @Autowired
    private IDabaTableService dabaTableService;

    @GetMapping()
    public String table() {
        return prefix + "/table";
    }


    @GetMapping("/{databaseId}")
    public String devicemanagement(ModelMap modelMap, @PathVariable("databaseId") String databaseId) {
        modelMap.put("databaseId", databaseId);
        try {

            DabaTable dabaTable = new DabaTable();
            dabaTable.setDatabaseId(Long.valueOf(databaseId));
            dabaTableService.deleteDabaTableByDataId(Long.valueOf(databaseId));
            DatabaseDetails datails = databaseDetailsService.selectDatabaseDetailsByDatabaseId(Long.valueOf(databaseId));
            List<String> list = MysqlLink.getAllDatabase(datails.getIp(), datails.getPort(), datails.getRoot(), datails.getPassword(), "1");
            assert list != null;
            for (String s : list) {
                dabaTable.setDataName(s);
                dabaTableService.insertDabaTable(dabaTable);
            }
            sleep(200);
            return prefix + "/table";
        } catch (Exception e) {
            return prefix + "/errortable";
        }
    }

    /**
     * 查询子数据库列表
     */
    @PostMapping("/list/{databaseId}")
    @ResponseBody
    public TableDataInfo list(@PathVariable("databaseId") String databaseId, DabaTable dabaTable) {

        dabaTable.setDatabaseId(Long.valueOf(databaseId));
        startPage();
        dabaTable.setDataName(dabaTable.getDataName());

        List<DabaTable> list = dabaTableService.selectDabaTableList(dabaTable);
        return getDataTable(list);
    }


}
